i'm trying data sendgrid account via api. i'm using classic asp. found some code works except need add authorization sendgrids api described in documentation.
i've found several examples seem suggest need add xmlhttp.setrequestheader after xmlhttp.open , before xmlhttp.send when uncomment line "xmlhttp.setrequestheader" below 500 error in browser. can tell me add authorization part xmlhttp object?
edit clarity: when comment line "xmlhttp.setrequestheader..." script runs , returns expected json result says need authenticate. when uncomment line 500 error. have replaced working api key (tested curl command) generic placeholder in code below. real key in place in file on server.
heres code i'm using:
<%@ language=vbscript %> <% set xmlhttp = createobject("msxml2.serverxmlhttp.6.0") xmlhttp.open "get", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0 http/1.1", false 'xmlhttp.setrequestheader "authorization", "bearer my-correct-sendgrid-api-key" xmlhttp.send "" response.addheader "content-type", "application/json;charset=utf-8" response.charset = "utf-8" pagereturn = xmlhttp.responsetext set xmlhttp = nothing response.write pagereturn %>
it seems basics working because code above returns expected json results, error message says need authenticate:
{ errors: [ { field: null, message: "authorization required" } ] }
sendgrids documentation uses example:
get https://api.sendgrid.com/v3/resource http/1.1 authorization: bearer your.api.key-here
thanks everyone! credit goes kul-tigin pointing out error, misinterpreting comment "http/1.1" part of url in sendgrids example.
when changed:
xmlhttp.open "get", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0 http/1.1", false xmlhttp.setrequestheader "authorization", "bearer my-correct-sendgrid-api-key"
to:
xmlhttp.open "get", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", false xmlhttp.setrequestheader "authorization", "bearer my-correct-sendgrid-api-key"
it started working without errors. here's working code:
<!-- language: lang-js --> <%@ language=vbscript %> <% set xmlhttp = createobject("msxml2.serverxmlhttp.6.0") xmlhttp.open "get", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", false xmlhttp.setrequestheader "authorization", "bearer my-correct-sendgrid-api-key" xmlhttp.send "" response.addheader "content-type", "application/json;charset=utf-8" response.charset = "utf-8" pagereturn = xmlhttp.responsetext set xmlhttp = nothing response.write pagereturn %>
searchandresq i'll read link provided on error trapping. thanks!