Hi,
I'm trying to use the arcgis rest api to upload kml file to the portal. I made addItem request and then addPart request to upload the file itself. The documentation does not mention how to send the file, so I read the content of the file as text and send it but I'm getting Internal Server Error response.
Here is my code (I also added the js file as attachment):
<SPAN class="comment token">// get list of files</SPAN>
<SPAN class="keyword token">var</SPAN> files <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">.</SPAN>fileSelect<SPAN class="punctuation token">.</SPAN>files<SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// continue only if the user select one (or more) files</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>files<SPAN class="punctuation token">.</SPAN>length <SPAN class="operator token">></SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">this</SPAN><SPAN class="punctuation token">.</SPAN>uploadButton<SPAN class="punctuation token">.</SPAN>innerHTML <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Uploading.."</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> file <SPAN class="operator token">=</SPAN> files<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="token function">fetch</SPAN><SPAN class="punctuation token">(</SPAN>'https<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">/</SPAN><SPAN class="operator token">/</SPAN>www<SPAN class="punctuation token">.</SPAN>arcgis<SPAN class="punctuation token">.</SPAN>com<SPAN class="operator token">/</SPAN>sharing<SPAN class="operator token">/</SPAN>rest<SPAN class="operator token">/</SPAN>content<SPAN class="operator token">/</SPAN>users<SPAN class="comment token">/*****/</SPAN>addItem'<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>
method<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'post'</SPAN><SPAN class="punctuation token">,</SPAN>
headers<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">'Content-Type'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'application/x-www-form-urlencoded;charset=UTF-8'</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
body<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'token='</SPAN><SPAN class="operator token">+</SPAN><SPAN class="token function">encodeURIComponent</SPAN><SPAN class="punctuation token">(</SPAN>token<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">'&f=json&title='</SPAN><SPAN class="operator token">+</SPAN><SPAN class="token function">encodeURIComponent</SPAN><SPAN class="punctuation token">(</SPAN>file<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">'&type=KML&filename='</SPAN><SPAN class="operator token">+</SPAN><SPAN class="token function">encodeURIComponent</SPAN><SPAN class="punctuation token">(</SPAN>file<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">'&multipart='</SPAN><SPAN class="operator token">+</SPAN><SPAN class="token function">encodeURIComponent</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> res<SPAN class="punctuation token">.</SPAN><SPAN class="token function">json</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Promise</SPAN><SPAN class="punctuation token">(</SPAN>resolve <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> id <SPAN class="operator token">=</SPAN> res<SPAN class="punctuation token">.</SPAN>id<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> fileReader <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">FileReader</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// when finish to read the file, make request with the content of the file</SPAN>
fileReader<SPAN class="punctuation token">.</SPAN>onload <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>e<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">let</SPAN> data <SPAN class="operator token">=</SPAN> e<SPAN class="punctuation token">.</SPAN>target<SPAN class="punctuation token">.</SPAN>result<SPAN class="punctuation token">;</SPAN>
<SPAN class="token function">fetch</SPAN><SPAN class="punctuation token">(</SPAN>'https<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">/</SPAN><SPAN class="operator token">/</SPAN>www<SPAN class="punctuation token">.</SPAN>arcgis<SPAN class="punctuation token">.</SPAN>com<SPAN class="operator token">/</SPAN>sharing<SPAN class="operator token">/</SPAN>rest<SPAN class="operator token">/</SPAN>content<SPAN class="operator token">/</SPAN>users<SPAN class="comment token">/****/</SPAN>items<SPAN class="operator token">/</SPAN><SPAN class="string token">'+id+'</SPAN><SPAN class="operator token">/</SPAN>addPart'<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>
method<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'post'</SPAN><SPAN class="punctuation token">,</SPAN>
headers<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">'Content-Type'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'application/x-www-form-urlencoded;charset=UTF-8'</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
body<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'token='</SPAN><SPAN class="operator token">+</SPAN>token<SPAN class="operator token">+</SPAN><SPAN class="string token">'&f=json&partNum=1&file='</SPAN><SPAN class="operator token">+</SPAN><SPAN class="token function">encodeURIComponent</SPAN><SPAN class="punctuation token">(</SPAN>data<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="token function">resolve</SPAN><SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token">// read file content</SPAN>
fileReader<SPAN class="punctuation token">.</SPAN><SPAN class="token function">readAsText</SPAN><SPAN class="punctuation token">(</SPAN>file<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> res<SPAN class="punctuation token">.</SPAN><SPAN class="token function">json</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// THIS LINE LOGS "500 - INTERNAL SERVER ERROR..."</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">.</SPAN>success<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token function">fetch</SPAN><SPAN class="punctuation token">(</SPAN>'https<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">/</SPAN><SPAN class="operator token">/</SPAN>www<SPAN class="punctuation token">.</SPAN>arcgis<SPAN class="punctuation token">.</SPAN>com<SPAN class="operator token">/</SPAN>sharing<SPAN class="operator token">/</SPAN>rest<SPAN class="operator token">/</SPAN>content<SPAN class="operator token">/</SPAN>users<SPAN class="comment token">/*****/</SPAN>items<SPAN class="operator token">/</SPAN><SPAN class="string token">'+id+'</SPAN><SPAN class="operator token">/</SPAN>commit'<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>
method<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'post'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">null</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// ...THEN GOES HERE</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> res<SPAN class="punctuation token">.</SPAN><SPAN class="token function">json</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">return</SPAN> undefined<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// ...AND THEN RETURN UNDEFINED!</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>res <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><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></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>Could someone please explain how to upload small file (kml, less than 5mb) to the portal using arcgis rest api (javascript)?
Thank you!
p.s sorry if I have grammar mistakes (I'm not native english speaker) ..