RestSharp请求体JSON格式问题:ESRI地理编码服务批量地址调用失败排查
Fixing the 400 Error When Using Request Body for ArcGIS Bulk Geocoding
Let's break down why your modified POST request is failing and get your bulk geocoding working for 500+ addresses.
The Root Cause of Your 400 Error
Your main issues are two critical mistakes in how you're handling the request body:
- You're URL-encoding the JSON content — this is only required when passing data in the URL query string, not in a POST request body. Encoding turns your valid JSON into a string that the ArcGIS service can't parse as the
addressesstructure it expects. - You're passing an encoded string to
AddJsonBody— RestSharp'sAddJsonBodymethod is designed to take an object (like yourJObject) that it can serialize to JSON itself. Passing a pre-encoded string results in a malformed request body the service can't interpret.
Corrected Code
Here's the fixed version of your code that should handle bulk addresses without issues:
SqlDataAdapter geoA = new SqlDataAdapter(SQLStatement, GEOconn); DataSet GeoDS = new DataSet(); geoA.Fill(GeoDS, "records"); // Build the correct JObject structure (same as before, no changes here) var obj = JObject.FromObject(GeoDS); obj["records"] = new JArray(obj["records"].Select(jo => new JObject(new JProperty("attributes", jo)))); // Keep the base URL without appending addresses parameter var con1 = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?f=pjson&token=abc123&outFields=ResultID,Status,Score,Addr_type,ShortLabel,City,Subregion,RegionAbbr,Postal,PostalExt,Country,X,Y"; var client = new RestClient(con1); client.Timeout = -1; var request = new RestRequest(Method.POST); // Pass the raw JObject directly to AddJsonBody — RestSharp handles serialization and Content-Type header request.AddJsonBody(obj); request.AddHeader("Cookie", "AGS_ROLES=abc123"); IRestResponse response = client.Execute(request); ESRIcls esri = JsonConvert.DeserializeObject<ESRIcls>(response.Content);
Key Changes Explained
- Removed URL encoding: The POST request body is meant to carry raw JSON, so encoding it was corrupting the data the ArcGIS service receives.
- Passed the JObject directly to AddJsonBody: This method automatically serializes the object to valid JSON and sets the correct
Content-Type: application/jsonheader — a requirement for the ArcGIS service to properly parse your address data.
Additional Debugging Tips
If you still run into issues:
- Check the full error response: The
response.Contentwill often include more specific details from ArcGIS (like which part of the request is invalid) that can help you narrow down the problem. - Test with a tool like Postman first: Copy your sample JSON into a POST request, set the
Content-Typeheader toapplication/json, and send it to the same URL. If this works, the issue is definitely in your code's request formatting. - Verify token validity: Expired or invalid tokens can also return 400 errors with vague messages, so double-check your token is active.
内容的提问来源于stack exchange,提问作者jpbartle
相关产品推荐
相关产品推荐

