QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: SpriggsySpriggs on July 21, 2020, 03:45:07 pm
-
I'm working on online API request usage and one in particular is stumping me because rather than using query URL they are using a POST request with headers and such. Anyone have a reliable function for such things?
-
I only remember people using GET with HTTP in QB64 examples.
-
@FellippeHeitor Here is the API I'm trying to build a request for:
https://sentim-api.herokuapp.com/ (https://sentim-api.herokuapp.com/)
It requires a POST HTTP request. I've been looking into PowerShell as a possible option but I saw that Windows had a WinHttpOpenRequest function but I can't figure out how to use the dang thing. If I can just build a string and do it with urlmon it would be much simpler
-
Just put the form data into the request body instead of the URL, in other words the data is POST (behind) the regular HTTP request. (see here: https://www.w3schools.com/tags/ref_httpmethods.asp)
POST /send.php HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: image/gif, image/jpeg, */*
Content-type: application/x-www-form-urlencoded
Content-length: 51
Connection: close
Name=Wei%C3%9Fes+R%C3%B6ssl&Ort=St.+Wolfgang&PLZ=5360
And here is all you wanna know about the HTTP protocol: https://www.w3.org/Protocols/rfc2616/rfc2616.html
-
@RhoSigma I've tried this in different combinations to no avail before making this post. It simply isn't working when I do it. I'm not sure what the issue is. I've even done it using PowerShell and no luck.
-
Well, I assume you didn't use the example as is, but replaced it with your desired data ?
1. forget about the Accept: line, as you probably wanna receive plain text
2. User-Agent: and Connection: are optional as well
3. important is to finish all lines with CR+LF
4. request body must be seperated from request header by an empty line
5. Content-Length: is the length of the request body starting after the empty line
Well, it might be, that the API in question does not only expect to use the POST method, but also the Content-Type: multipart/formdata (instead of the application/x-www-form-urlencoded), but I've never needed that encoding for my uses, hence I don't know the specs, but it's probably documented in the HTTP protocol (see link below the example in my last post).
-
I included the link to the API in a previous reply. Here it is again: https://sentim-api.herokuapp.com/ (https://sentim-api.herokuapp.com/)
It seems vague to me how they are wanting it formatted. And, yes, I would have been using my own data when running the tests. I've had no luck so far. I understand that each line needs to end in carriage return & line feed. I've been doing that. I've been trying with zero success to build this POST request. Knowing how one is formatted versus making it work. I have read those w3 articles prior to your reply and those weren't very helpful. Not sure if I should be using WinHTTPRequest and stress myself out trying to figure out how to use it or if should I be using a C++ header file or if I should I be using PowerShell. I have no problem with straight query strings but this POST request thing is stumping me.
-
They're quite precise; they want you to set the Accept and Content-Type headers to application/json, and send a json encoded object with the text attribute.
I tested it briefly with curl, it does work.
-
@luke Yes, I was able to build a curl string and do it online through a CURL tester but using it in curl through CMD I was unable. Got all sorts of errors. What did you do to make it work?
-
~$ curl https://sentim-api.herokuapp.com/api/v1/ --data '{"text":"this is horrible"}' -H "content-type: application/json" -H "accept: application/json"
{"result":{"polarity":-1.0,"type":"negative"},"sentences":[{"sentence":"this is horrible","sentiment":{"polarity":-1.0,"type":"negative"}}]}
-
@luke , Is that in Windows? It looks like a Linux terminal. I ran that command and got the same errors as before. "400 Bad Request"
-
Well, I rearranged the command and it instead just failed for a timeout. So I guess it is formatted correctly but I don't know why it is timing out.
-
Good grief! Finally! I used a CURL builder online and changed up the quotes and escaped the double quotes and it finally freaking worked! Thanks for your help! Now I can write my API code! ;)
-
Yes, that's a Linux command line. The Windows cmd.exe quoting & escaping rules are a strange and foreign land to me that I've never been able to understand.