Web Connection
POST raw
Gravatar is a globally recognized avatar based on your email address. POST raw
  Vincent H.
  All
  Sep 18, 2021 @ 03:48am

Hi Rick,

In CURL, I have this code:

curl --location --request POST 'http://127.0.0.1:2008/sa/api/lra/v1/actes/prevalider' \
--header 'Content-Type: application/json' \
--data-raw '{
   "flux":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmN..."
}'

So, my VFP code is:

loHTTP = CREATEOBJECT ('wwHTTP')
WITH loHTTP
   .nConnectTimeOut = 30
   .lUseLargePostBuffer = .T.
   .addheader("Content-Type", "application/json")
   .nHttpPostMode = 2
   .cContentType = "application/json"
   .AddPostKey("flux", STRCONV (FILETOSTR (Myfile), 13))
ENDWITH

But I get an error related to the settings

I tried with or without .nHttpPostMode and .cContentType, without success

An idea ?

thanks in advance

Gravatar is a globally recognized avatar based on your email address. re: POST raw
  Rick Strahl
  Vincent H.
  Sep 18, 2021 @ 02:01pm

You don't need .addHeader() as setting the content type will set this automatically. Adding it this way adds two headers (which could potentially be rejected by the target server).

You're making posting JSON way too complicated:

loHttp = CREATEOBJECT("wwHttp")
loHttp.cContentType = "application/json"
lcResut = loHttp.Post(lcUrl, lcJson)

Note you do not want to pass 'form variables' but rather raw data. So you can use the .Post() syntax above, or if you use the old syntax:

loHttp = CREATEOBJECT("wwHttp")
loHttp.cContentType = "application/json"
loHttp.AddPostKey(lcJson)  && note: no key which sets the entire buffer
lcResult = loHttp.HttpGet(lcUrl)

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: POST raw
  Vincent H.
  Rick Strahl
  Sep 20, 2021 @ 12:08am

Thank you Rick.

It didn't work in my case. But "manually", no worries:

   .addheader ("Content-Type", "application / json")
   lcJSON = '{"flux":"Base64Base64..."}'
   .AddPostKey ("", lcJSON)
Gravatar is a globally recognized avatar based on your email address. re: POST raw
  Rick Strahl
  Vincent H.
  Sep 20, 2021 @ 09:28am

Are you on an old version that doesn't support cContentType? That should always set the header, and the parameterless AddPostKey() has been there for a long time as well (5.x at least).

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: POST raw
  Vincent H.
  Rick Strahl
  Sep 20, 2021 @ 09:34am

No, I am in 6.x with a support of cContentType

© 1996-2021