Multipart post request with d3.json () / d3.xhr ()

Currently there is no support for submitting data with multiple forms with a request?

I understand how to do POST with d3.json (). post () as described here , but I wanted to use POST to send API parameters via multipart / form-data.

It seems strange that I cannot find any resources on how best to do this; the closest I came is https://github.com/mbostock/d3/issues/929 and https://github.com/mbostock/d3/wiki/Requests , but in fact they do not cover multi-part forms.

Is there an undocumented part of the functionality described in No. 929 that I could not find in d3.v3.js that would allow the use of multipart forms? Is anyone currently working on this issue or are interested in it?

+4
source share
1 answer

There are three steps to a successful multi-page post.

  • Add Content-type: application/x-www-form-urlencoded header Content-type: application/x-www-form-urlencoded
  • Encode form data
  • Combine it as if you specified query strings in the URL

Then just send it as POST data.

None of this is specific to d3, but I thought I would give my answer and some sample code, since I landed here.

Code example:

 var xhr = d3.xhr(post_url) .header("Content-type", "application/x-www-form-urlencoded"); xhr.post("arg1=" + encodeURIComponent(arg1) + "&arg2=" + encodeURIComponent(arg2), function(error, result) { if(error) throw new Error(error); read_paths.data(JSON.parse(result.responseText)); }); 
+1
source

All Articles