http post request with a header and a json body?

Just starting out? Need help? Post your questions and find answers here.
User avatar
RK_aus_S
User
User
Posts: 11
Joined: Thu Apr 20, 2023 7:29 am
Contact:

http post request with a header and a json body?

Post by RK_aus_S »

Hello
I'm absolute new in PureBasic and struggle to get a working http post request.

This is the requirement I got for the http post:

URL: https://enlighten.enphaseenergy.com/log ... =xxxxxxxxx

Header:
"Accept": "application/json",
"content-type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache"

Json Body:
"user[email]": username,
"user[password]": password


I I tried a few things but nothing worked, like this:

Code: Select all

NewMap Header$()
  Header$("Accept") = "application/json"
  Header$("content-type") = "application/x-www-form-urlencoded"
  Header$("Cache-Control") = "no-cache"
  
NewMap Body$()
 Body$("user[email]") = "my_name@gmail.com"
 Body$("user[password]") = "xxxxxxxxxx" 
  
result = HTTPRequest(#PB_HTTP_Post, "https://enlighten.enphaseenergy.com/login/login.json?serialnumber=1234567890", Body$(), 0, Header$())
    Debug "StatusCode: " + HTTPInfo(result, #PB_HTTP_StatusCode)
    Debug "Response: " + HTTPInfo(result, #PB_HTTP_Response)
I expect to get a kind of session_id back. I guess, I made something wrong with the body, which should be JSON....?

Has anybody some ideas?
Kind regards
Roman
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: http post request with a header and a json body?

Post by DarkDragon »

You need to create a json first. You have a map, but HttpRequest wants the body to be a pure string.

https://www.purebasic.com/documentation ... ejson.html
bye,
Daniel
User avatar
RK_aus_S
User
User
Posts: 11
Joined: Thu Apr 20, 2023 7:29 am
Contact:

Re: http post request with a header and a json body?

Post by RK_aus_S »

Thank you.
I tryied something like this, but have still no success:

Code: Select all

If CreateJSON(0)
    Account = SetJSONObject(JSONValue(0))
    SetJSONString(AddJSONMember(Account, "user[email]"), "my_name@gmail.com")
    SetJSONString(AddJSONMember(Account, "user[password]"), "xxxxxxxx")
    Body$ = ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf

NewMap Header$()
  Header$("Accept") = "application/json"
  Header$("content-type") = "application/x-www-form-urlencoded"
  Header$("Cache-Control") = "no-cache"
  
  
result = HTTPRequest(#PB_HTTP_Post, "https://enlighten.enphaseenergy.com/login/login.json?serialnumber=1234567890", Body$, 0, Header$())
    Debug "StatusCode: " + HTTPInfo(result, #PB_HTTP_StatusCode)
    Debug "Response: " + HTTPInfo(result, #PB_HTTP_Response)
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: http post request with a header and a json body?

Post by infratec »

Code: Select all

username$ = "uname"
password$ = "pword"

Body$ = ~"{\"user[email]}\": \"" + username$ + ~"\","
Body$ + ~"\"user[password]\": \"" + password$ + ~"\"}"

Debug Body$

infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: http post request with a header and a json body?

Post by infratec »

You forgot FreeJSON() :wink:

Maybe you need to encode the body cause of the @

Try UrlEncoder() because this is recommended by your header.
Last edited by infratec on Thu Jun 20, 2024 6:53 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: http post request with a header and a json body?

Post by infratec »

Best is to make a real access in a web browser and open before the developer Tools with F12
Then you can inspect what is sendet, or use wireshark and replace https with http to see the request.
hoangdiemtinh
User
User
Posts: 97
Joined: Wed Nov 16, 2022 1:51 pm

Re: http post request with a header and a json body?

Post by hoangdiemtinh »

That's right which I find for along time !!
PC: Windows 10 x64, 8GB RAM. PB ver: 6.x
--
I love PB5 vs PB6 :)
User avatar
RK_aus_S
User
User
Posts: 11
Joined: Thu Apr 20, 2023 7:29 am
Contact:

Re: http post request with a header and a json body?

Post by RK_aus_S »

Thanks to all.

I know the API documatation - and unfortunately, none of your tips worked.

I used the Postman API Tool https://www.postman.com/home to check the specified values for the header and the body - and it works very well. it even works, when the body format is "form-data" instead of "x-www-form-urlencoded".

Here the same URS, header and body, entered in Postman

Image

Image

I clearly get a success and the expected "session_id".


So there must be indeed something wrong with my PureBasic approach.
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: http post request with a header and a json body?

Post by DarkDragon »

RK_aus_S wrote: Fri Jun 21, 2024 6:28 am Thanks to all.

I know the API documatation - and unfortunately, none of your tips worked.

I used the Postman API Tool https://www.postman.com/home to check the specified values for the header and the body - and it works very well. it even works, when the body format is "form-data" instead of "x-www-form-urlencoded".

Here the same URS, header and body, entered in Postman

Image

Image

I clearly get a success and the expected "session_id".


So there must be indeed something wrong with my PureBasic approach.
Form data isn't json. "say=Hi&to=Mom" that's how form data looks.
bye,
Daniel
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: http post request with a header and a json body?

Post by Bisonte »

I was just about to say, since when is the request a json ? the result is a json! :mrgreen:
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
RK_aus_S
User
User
Posts: 11
Joined: Thu Apr 20, 2023 7:29 am
Contact:

Re: http post request with a header and a json body?

Post by RK_aus_S »

Thank you very much, but you are speaking a bit too "high level" for me...
In my opening post, I didn't code the body as JSON but rather as a map like the header - but that didn't work either.
How do I have to code the body correctly, I'm clueless...?

BTW, I'm not a programmer, neither professionally nor privately. Yes, I've done a few small things in AutoIt, PowerShell, AVR-C and JavaScript - but I'm not really good or experienced at any of them...
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: http post request with a header and a json body?

Post by DarkDragon »

RK_aus_S wrote: Fri Jun 21, 2024 6:57 am In my opening post, I didn't code the body as JSON
You wrote "Json Body:" 😉 that's why we assumed you wanted json. Anyway, you'd simply stitch together a string.

Something like this

Code: Select all

URLEncoder("user[email]") + "=" + URLEncoder(usermail$) + "&" + URLEncoder("user[password]") + "=" + URLEncoder(password$)
bye,
Daniel
User avatar
RK_aus_S
User
User
Posts: 11
Joined: Thu Apr 20, 2023 7:29 am
Contact:

Re: http post request with a header and a json body?

Post by RK_aus_S »

I'm very sorry about that. I apologize.
I definitely got this requierements which mentioned "JSON Body". Of course I see now, this was misleading extremely, but I didn't know better.
Sorry again!

But now it works! It's great when experienced users can solve a problem so quickly (as long as they get correct information ;-) )
Many thanks to all, und you, Daniel.

Kind regards
Roman
Post Reply