You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. 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 addresses structure it expects.
  2. You're passing an encoded string to AddJsonBody — RestSharp's AddJsonBody method is designed to take an object (like your JObject) 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/json header — 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.Content will 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-Type header to application/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:02:50