Select to view content in your preferred language

ArcGIS Server REST API HTTP POST Returning HTML form instead of JSON

5618
2
Jump to solution
09-18-2017 09:41 AM
TylerWaring
Frequent Contributor

I am accessing ArcGIS Server REST API geometry server in a Java application to do some analysis. I expect to be buffering lines and polygons and plan to use a post request due to the length restrictions on a 'GET' request. I have structured my HttpRequest's endpoint and body correctly and verified my response using Postman  v5.2.0 which is powered by Asp.Net. 

I am using Java to make requests to the geometry server. When I make a geometry server buffer request from my Java application, I get a 200 status. However, the response from the server is not a buffer response. Instead, my response is the HTML form from the buffer tool's endpoint. I.E. : 

<html lang="en">
<head>
<title>Buffer (GeometryServer)</title>
<link href="https://community.esri.com/arcgis1/rest/static/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<table width="100%" class="userTable">
<tr>
<td class="titlecell">
ArcGIS REST Services Directory
</td>
<td align="right"> .......

I did a quick internet search and found a related post on esri's resource-proxy's GitHub page: POST to geometry service for 'PROJECT' is returning HTML instead of JSON · Issue #332 · Esri/resourc...  

Is this issue on the resource-proxy GitHub site related to making REST endpoint POST requests from a Java application? 

The Java code that I am using to perform the POST request is:

      Http httpProtocol = new Http();
      HttpRequest request = new HttpRequest();

      String reqBody = 'geometries=-78.868242630049537,35.773265772338988&inSR=4326&outSR=4326&bufferSR=102113&distances=1000&f=json';
      Integer bodyLength = reqbody.length();

      request.setEndPoint('http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/buffer');
      request.setMethod('POST');

      request.setBody(reqBody);
      request.setCompressed(true);
      request.setHeader('Accept', 'application/json');
      request.setHeader('Content-type', 'application/json' );
      request.setHeader('Content-Length', String.valueOf(bodyLength));
      HttpResponse response = httpProtocol.send(request);
      system.debug('response.getBody(): ' + response.getBody());
      system.debug('response: ' + response);

Has anyone else had this issue or know of a work around? Any help or suggestions will be greatly appreciated. 

Thanks, Tyler 

 

0 Kudos
1 Solution

Accepted Solutions
TylerWaring
Frequent Contributor

Greetings, 

It turns out that both the 'Accept' and 'Content-Type' headers need to be set to 'application/x-www-form-urlencoded' . 

i.e

request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setHeader('Accept', 'application/x-www-form-urlencoded')

 

Thanks to Nick from ESRI support 

Best, Tyler 

P.S. also be sure not to compress the data. i.e.

request.setCompressed(true);

you can however set the content length. i.e.

request.setHeader('Content-Length', String.valueOf(bodyLength));

View solution in original post

2 Replies
TylerWaring
Frequent Contributor

Greetings, 

It turns out that both the 'Accept' and 'Content-Type' headers need to be set to 'application/x-www-form-urlencoded' . 

i.e

request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setHeader('Accept', 'application/x-www-form-urlencoded')

 

Thanks to Nick from ESRI support 

Best, Tyler 

P.S. also be sure not to compress the data. i.e.

request.setCompressed(true);

you can however set the content length. i.e.

request.setHeader('Content-Length', String.valueOf(bodyLength));

BrendonO_Hanlon
New Contributor

I am getting a similar thing with the server returning the query form, but no response. I've tried various combinations of the request parameters (using PHP) but the main parts of the code are:

$fields = json_encode($payload);

$headers = array(
'Accept: application/x-www-form-urlencoded',
'Content-Type: application/x-www-form-urlencoded'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPGET => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array('f'=>'html','features'=>$fields)
);


curl_setopt_array( $ch, $options );
$response = curl_exec($ch);

-----------

My result is often returned as either the form, or with other variations of the POSTFIELDS array - either NULL, or empty.

I can paste the values into the addFeatures page within the online interface, and the same 'features' parameter works from the box.

Does anybody have any ideas why this might be happening. I''ve also tried json verions for the Accept and Content-Type headers.

0 Kudos