easypost - Getting a 401 when making a HTTP POST request using CORS in Dart -
i trying build small web app in dart. app calls easypost web service, illustrated curl call:
$ curl -x post https://myuserid@api.easypost.com/v2/trackers -d 'tracker[tracking_code]=ez1000000001' -d 'tracker[carrier]=fedex'
here dart code attempted match request:
// configuration parameters var url = "https://myuserid@api.easypost.com/v2/trackers"; var data = { 'tracker[tracking_code]': '$packageno', 'tracker[carrier]': 'fedex' }; // make request var request = new httprequest(); request ..open("post", url, async: true) ..withcredentials = true ..onloadend.listen((_) => print(request.responsetext)) ..send(data.tostring());
when run in webstorm code fails when making request cors error:
post https://api.easypost.com/v2/trackers 401 (unauthorized) xmlhttprequest cannot load https://api.easypost.com/v2/trackers. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:63342' therefore not allowed access. response had http status code 401.
isn't supposed handled automatically? or need add headers request myself?
the headers need added server, otherwise client won't make request. post
request browser makes preflight request check cors headers. if not added server, browser doesn't make actual post
request.
if server doesn't add these headers it's not intended used way.
there possible workarounds: http://usamadar.com/2012/06/24/getting-around-browsers-same-origin-policy-sop-with-proxies-script-injection-jsonp-and-cors/
Comments
Post a Comment