West Wind Internet and Client Tools
HTTP Post request
Gravatar is a globally recognized avatar based on your email address. HTTP Post request
  Robert
  All
  Mar 26, 2019 @ 09:35am

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;
}
Gravatar is a globally recognized avatar based on your email address. re: HTTP Post request
  Rick Strahl
  Robert
  Mar 26, 2019 @ 03:18pm

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 ---

Gravatar is a globally recognized avatar based on your email address. re: HTTP Post request
  Robert
  Rick Strahl
  Mar 26, 2019 @ 03:48pm

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!!!

Gravatar is a globally recognized avatar based on your email address. re: HTTP Post request
  Rick Strahl
  Robert
  Mar 26, 2019 @ 11:28pm

Yeah probably not the header - that's operational. More likely you had nPostMode=2 that will not work as the data gets reformatted.

Glad it's working...

+++ Rick ---

© 1996-2019