Page 1 of 1
http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:11 pm
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
Re: http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:26 pm
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
Re: http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:44 pm
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)
Re: http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:45 pm
by infratec
Code: Select all
username$ = "uname"
password$ = "pword"
Body$ = ~"{\"user[email]}\": \"" + username$ + ~"\","
Body$ + ~"\"user[password]\": \"" + password$ + ~"\"}"
Debug Body$
Re: http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:48 pm
by infratec
You forgot FreeJSON()
Maybe you need to encode the body cause of the @
Try UrlEncoder() because this is recommended by your header.
Re: http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:50 pm
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.
Re: http post request with a header and a json body?
Posted: Thu Jun 20, 2024 6:59 pm
by infratec
Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 3:07 am
by hoangdiemtinh
That's right which I find for along time !!
Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 6:28 am
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
I clearly get a success and the expected "session_id".
So there must be indeed something wrong with my PureBasic approach.
Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 6:37 am
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
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.
Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 6:42 am
by Bisonte
I was just about to say, since when is the request a json ? the result is a json!

Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 6:57 am
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...
Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 7:20 am
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$)
Re: http post request with a header and a json body?
Posted: Fri Jun 21, 2024 7:53 am
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