Hello Rick, I am trying to issue a POST request but keep getting the error: "status_code":405,"error_message":"Method "GET" not allowed." I used Postman to build the request and test it successfully. I will include the sample PHP code that Postman generated below. I was using version 6 of wwHttp and yesterday I upgraded to wwHTTP v.7 but still get the same error using the new POST method. Any thoughts?
oHTTP=CREATEOBJECT("wwHttp")
oHttp.cContentType = "application/x-www-form-urlencoded"
oHTTP.nHTTPPostMode = 2
oHttp.cHttpVerb = 'POST'
oHttp.lIgnoreCertificateWarnings = .T.
oHttp.cPostbuffer = '{"username": "MyUser", "password": "Secret"}'
lcURL = "api.somedomain.com/api/rest/v001/login/"
cHTMLResult = oHTTP.post(lcURL)
I tried various iterations of cContentType, cPostbuffer and / or providing oHttp username and password opposed to cPostbuffer e.g.
oHttp.cContentType = "application/json"
lcJson = '{"username": "MyUser", "password": "Secret"}'
oHTTP.AddPostKey(lcJson)
or
oHttp.AddPostKey("username","MyUser",.F.)
oHttp.AddPostKey("password","Secret",.F.)
oHttp.Nhttpconnecttype=1
or
oHttp.cusername = "MyUser"
oHttp.cPassword = "Secret"
Sample PHP that works:
<?php
$request = new HttpRequest();
$request->setUrl('https://api.somedomain.com/api/rest/v001/login');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'Content-Type' => 'application/json'
));
$request->setBody('{"username": "MyUser", "password": "Secret"}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
You need to change the content type to application/json
and remove the nHttpPostMode
flag.
Try this:
oHTTP=CREATEOBJECT("wwHttp")
oHttp.cContentType = "application/json"
oHttp.AddHeader('cache-control','no-cache')
oHttp.AddPostKey('{"username": "MyUser", "password": "Secret"}')
lcURL = "https://api.somedomain.com/api/rest/v001/login/"
cHTMLResult = oHTTP.post(lcURL)
+++ Rick ---
Thank you. That did it. I had tried changing the content type to JSON as you suggested but I bet it was adding the additional header that did the trick. Thanks again!!!