Encountered trouble today trying to query a hosted feature service and sending the results as new features to an identical (same fields/configuration) hosted feature service using Python (requests). For the sake of this example, I reduced my code that produces the error to the example below.
<SPAN class="keyword token">import</SPAN> json
url <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"https://services9.arcgis.com/ty0YDnowzprP81CR/ArcGIS/rest/services/testlayer/FeatureServer/0"</SPAN>
url_query <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"https://services9.arcgis.com/ty0YDnowzprP81CR/ArcGIS/rest/services/testlayer/FeatureServer/0/query"</SPAN>
url_token <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"https://www.arcgis.com/sharing/generateToken"</SPAN>
referer <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"https://www.arcgis.com/"</SPAN>
<SPAN class="comment token"># query from featureservice 1</SPAN>
token_endpoint <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"https://www.arcgis.com/sharing/generateToken"</SPAN>
querystring <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"pjson"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"client"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"referer"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"referer"</SPAN><SPAN class="punctuation token">:</SPAN> referer<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"expiration"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"60"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"username"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"<username>"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"password"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"<password>"</SPAN><SPAN class="punctuation token">}</SPAN>
response <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>token_endpoint<SPAN class="punctuation token">,</SPAN> data<SPAN class="operator token">=</SPAN>querystring<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>json<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
token <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">]</SPAN>
feature_querystring <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">"where"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"1=1"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"objectIds"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"outFields"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"returnGeometry"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"true"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"json"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> str<SPAN class="punctuation token">(</SPAN>token<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN>
response_featuredata <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>request<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"GET"</SPAN><SPAN class="punctuation token">,</SPAN> url_query<SPAN class="punctuation token">,</SPAN> params<SPAN class="operator token">=</SPAN>feature_querystring<SPAN class="punctuation token">)</SPAN>
response_featuredata_dict <SPAN class="operator token">=</SPAN> response_featuredata<SPAN class="punctuation token">.</SPAN>json<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
features <SPAN class="operator token">=</SPAN> response_featuredata_dict<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"features"</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># copy to featureservice 2</SPAN>
add_features_url <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"https://services9.arcgis.com/ty0YDnowzprP81CR/arcgis/rest/services/testlayer2/FeatureServer/0/addFeatures"</SPAN>
params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"features"</SPAN><SPAN class="punctuation token">:</SPAN> json<SPAN class="punctuation token">.</SPAN>dumps<SPAN class="punctuation token">(</SPAN>features<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> token<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"pjson"</SPAN>
<SPAN class="punctuation token">}</SPAN>
response <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>url<SPAN class="operator token">=</SPAN>add_features_url<SPAN class="punctuation token">,</SPAN> params<SPAN class="operator token">=</SPAN>params<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>response<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>response<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The above code results in the following repsonse:
<SPAN class="doctype token"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>html</SPAN> <SPAN class="attr-name token">xmlns</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>http://www.w3.org/1999/xhtml<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>head</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>meta</SPAN> <SPAN class="attr-name token">http-equiv</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>Content-Type<SPAN class="punctuation token">"</SPAN></SPAN> <SPAN class="attr-name token">content</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>text/html; charset<SPAN class="punctuation token">=</SPAN>iso-8859-1<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">/></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>title</SPAN><SPAN class="punctuation token">></SPAN></SPAN>404 - File or directory not found.<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>title</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>style</SPAN> <SPAN class="attr-name token">type</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>text/css<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="comment token"><!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
--></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>style</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>head</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>body</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>div</SPAN> <SPAN class="attr-name token">id</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>header<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>h1</SPAN><SPAN class="punctuation token">></SPAN></SPAN>Server Error<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>h1</SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>div</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>div</SPAN> <SPAN class="attr-name token">id</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>content<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>div</SPAN> <SPAN class="attr-name token">class</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>content-container<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>fieldset</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>h2</SPAN><SPAN class="punctuation token">></SPAN></SPAN>404 - File or directory not found.<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>h2</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>h3</SPAN><SPAN class="punctuation token">></SPAN></SPAN>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>h3</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>fieldset</SPAN><SPAN class="punctuation token">></SPAN></SPAN><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>div</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>div</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>body</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>html</SPAN><SPAN class="punctuation token">></SPAN></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Which is weird, because when I try to manually insert the features into the addFeatures-endpoint via the browser, everything works just fine.
Something else I noticed; when I change:
params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"features"</SPAN><SPAN class="punctuation token">:</SPAN> json<SPAN class="punctuation token">.</SPAN>dumps<SPAN class="punctuation token">(</SPAN>features<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> token<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"pjson"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>into:
params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"features"</SPAN><SPAN class="punctuation token">:</SPAN> json<SPAN class="punctuation token">.</SPAN>dumps<SPAN class="punctuation token">(</SPAN>features<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"token"</SPAN><SPAN class="punctuation token">:</SPAN> token<SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"f"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"pjson"</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>The code works fine (but only sends the first of the list of features to the endpoint). Does anyone have an idea about what i'm doing wrong? I'm guessing it has something to do with the way the dict/json is encoded before sending it.
Thanks in advance