Author Topic: Anyone know how to build an HTTP POST request?  (Read 4848 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Anyone know how to build an HTTP POST request?
« 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?
Shuwatch!

FellippeHeitor

  • Guest
Re: Anyone know how to build an HTTP POST request?
« Reply #1 on: July 21, 2020, 03:46:09 pm »
I only remember people using GET with HTTP in QB64 examples.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #2 on: July 21, 2020, 03:55:50 pm »
@FellippeHeitor Here is the API I'm trying to build a request for:
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
Shuwatch!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Anyone know how to build an HTTP POST request?
« Reply #3 on: July 21, 2020, 05:56:09 pm »
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)
Code: [Select]
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
« Last Edit: July 21, 2020, 06:10:04 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #4 on: July 21, 2020, 06:32:59 pm »
@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.
Shuwatch!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Anyone know how to build an HTTP POST request?
« Reply #5 on: July 21, 2020, 07:07:44 pm »
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).
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #6 on: July 21, 2020, 07:28:22 pm »
I included the link to the API in a previous reply. Here it is again: 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.
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Anyone know how to build an HTTP POST request?
« Reply #7 on: July 21, 2020, 08:32:07 pm »
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.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #8 on: July 21, 2020, 09:24:02 pm »
@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?
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Anyone know how to build an HTTP POST request?
« Reply #9 on: July 21, 2020, 10:43:38 pm »
Code: [Select]
~$ 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"}}]}

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #10 on: July 22, 2020, 07:20:14 am »
@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"
« Last Edit: July 22, 2020, 07:25:10 am by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #11 on: July 22, 2020, 07:38:37 am »
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.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Anyone know how to build an HTTP POST request?
« Reply #12 on: July 22, 2020, 07:43:25 am »
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! ;)
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Anyone know how to build an HTTP POST request?
« Reply #13 on: July 22, 2020, 08:12:35 am »
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.