.net - How to flash a spark core from C# -
i trying flash spark core c# application. keep getting { error: nothing do? }
response.
below code
var url = string.format("https://api.spark.io/v1/devices/{0}", sparkdeviceid); using (var client = new httpclient()) { client.defaultrequestheaders.authorization = new system.net.http.headers.authenticationheadervalue("bearer", accesstoken); using (var formdata = new multipartformdatacontent()) { httpcontent filecontent = new bytearraycontent(encoding.ascii.getbytes(rom)); //client.sendasync() formdata.add(filecontent, "file", "file"); var response = client.putasync(url, formdata).result; if (!response.issuccessstatuscode) throw new exception("an error occurred during rom flash!"); var responsestream = response.content.readasstreamasync().result; using (var reader = new streamreader(responsestream, true)) { var result = reader.readtoend(); } } return true; }
the documentation reads:
the api request should encoded multipart/form-data file field populated.
i believe problem endpoint doesn't see file. idea on how resolve this?
finally got working.
the issue way .net generated content-disposition header file form data.
i used fiddler compare output of successful put request put request code generating:
successful put request generated using curl:
put http://127.0.0.1:8888/ http/1.1 user-agent: curl/7.33.0 host: 127.0.0.1:8888 accept: */* content-length: 2861 expect: 100-continue content-type: multipart/form-data; boundary=------------------------5efcf64a370f13c8 --------------------------5efcf64a370f13c8 content-disposition: form-data; name="file"; filename="ms.ino" content-type: application/octet-stream ...
my put request (unsuccessful):
put https://api.spark.io/v1/devices/{deviceid} http/1.1 authorization: bearer {access_token} content-type: multipart/form-data; boundary="135f5425-9342-4ffa-a645-99c04834026f" host: api.spark.io content-length: 2878 expect: 100-continue --135f5425-9342-4ffa-a645-99c04834026f content-type: application/octet-stream content-disposition: form-data; name=file; filename=file.ino; filename*=utf-8''file.ino ...
if you'll notice difference in content-type actual file being sent:
successful: content-disposition: form-data; name="file"; filename="ms.ino"
unsuccessful: content-disposition: form-data; name=file; filename=file.ino; filename*=utf-8''file.ino
most specifically, resolution add quotes around name
attribute.
resolution:
formdata.add(filecontent, "\"file\"", "file.ino");
Comments
Post a Comment