1

I'm looking at implementing Summry, and they only give PHP API connection examples - is there any chance someone could turn it into a JS request for me?

I essentially just want it to parse the text I give it, nothing super fancy yet.

I've tried to see what CURLOPT_POSTFIELDS, and CURLOPT_HTTPHEADER match to in a JS request, to no avail. I'm probably looking in the wrong place, however.

PHP example

$long_article = "Long article text goes here";

$ch = curl_init("http://api.smmry.com/&SM_API_KEY=XXXXXXXXX&SM_LENGTH=14&SM_WITH_BREAK");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:")); // Important do not remove
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "sm_api_input=".$long_article);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$return = json_decode(curl_exec($ch), true);
curl_close($ch);

JavaScript replication

...
const request = require('request-promise');
...

const long_article = 'Long article text goes here';
const r = request({
  method: 'POST',
  uri: `http://api.smmry.com/&SM_API_KEY=${process.env.SMMRY_API_KEY}`,
  // headers: { Authorization: 'array(Expect:)' },
  // body: { sm_api_input: long_article },
  json: true,
});

request(r)
.then((parsedBody) => { debug(parsedBody); })
.catch((err) => { debug(err); });

The error I'm getting is { sm_api_error: 1, sm_api_message: 'INSUFFICIENT VARIABLES' } so I'm atleast hitting the right URL - so that's a start :)

Thanks in advance!

Ollie

1 Answer 1

1

The uri that you are passing to uri is attempting to use a Query String but you're not correctly formatting the URI you're requesting. A Query String should start with a ? and separate Key/Value pairs with a &. Your Query String is beginning with a & instead of a ?

Change:
http://api.smmry.com/&SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_LENGTH=14 &SM_WITH_BREAK

To:
http://api.smmry.com/?SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_LENGTH=14&SM_WITH_BREAK

Additionally, you're creating a request r and then passing that request to request. That's not how the request-promise module works. When invoking request(opts), the request to the URI will be made immediately and the corresponding Promise will be returned. Once the request is complete any chained handlers will be invoked (.then(), .catch(), .finally()).

const request = require('request-promise')
const sm_api_input = 'Long article text goes here'

request({
    method: 'POST',
    headers: {
        'Expect': '100-continue'
    },
    uri: `http://api.smmry.com/?SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_LENGTH=14&SM_WITH_BREAK`,
    form: {sm_api_input},
    json: true,
    timeout: 20000
})
  .then(body => debug)
  .catch(err => debug)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help :) It seems the QS is meant to be a '&' sign. The other api route seems to work flawlessly uri: http://api.smmry.com/&SM_API_KEY=${process.env.SMMRY_API_KEY}&SM_URL=http://www.bbc.co.uk/news/uk-england-oxfordshire-42238343 which is really odd. I've taken the request above (you need a pesky comma after uri), and I'm still getting the cryptic { sm_api_error: 1, sm_api_message: 'INSUFFICIENT VARIABLES' }. Thanks for your time :)
@Ollie Are you sure you're passing in all of the required Variables for the Request. Looking at those docs, your URI isn't correct in your request() opts. Look again at my answer and look at the updated URI and see if that works.
I've got it working! anything other than the api key is optional it seems :) If you change it from body to form it works flawlessly. uri: http://api.smmry.com/&SM_API_KEY=${process.env.SMMRY_API_KEY}, form: { sm_api_input }, Thank you so much for your time :) If you edit the question to that form + uri, I can mark you as correct :)
@Ollie ah, the request must be made using a form header instead of application/json. I've updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.