I am a self-taught and got stuck in a problem. If you can please give me a little help.
I'm doing a query via API on a website. I can create JSON but can't enter username and password.
They report (https://api.braspress.com/home):
Depending on the chosen language or platform you will need to 'encode' your username and password and pass them in the request header Authorization: Basic Y2xpZW50ZTpjbGllbnRl. If your language or library already does this internally skip this step. For other cases, use the website https://www.base64encode.org
curl usage:
curl -v -H "Authorization: Basic Y2xpZW50ZTpjbGllbnRl" -H "Content-Type: application/json" -d '{"cnpjRemetente":60701190000104,"cnpjDestinatario":30539356867,"modal":"R","tipoFrete":"1", "cepOrigem":2323000,"cepDestino":7093090,"vlrMercadoria":100.00,"peso":50.55,"volumes":100}, "cubagem":[{"altura":0.46,"largura":0.67,"comprimento":0.67,"volumes":10}]' -X POST https://api.braspress.com/v1/cotacao/calcular/json
javascript usage
var authorizationBasic = 'Y2xpZW50ZTpjbGllbnRl';
$.ajax({
type: 'POST',
url: 'https://api.braspress.com/v1/cotacao/calcular/json',
data: '{"cnpjRemetente":60701190000104,"cnpjDestinatario":30539356867,"modal":"R","tipoFrete":"1", "cepOrigem":2323000,"cepDestino":7093090,"vlrMercadoria":100.00,"peso":50.55,"volumes":10,"cubagem":[{"altura":0.46,"largura":0.67,"comprimento":0.67,"volumes":10}]}',
dataType: "json",
contentType: 'application/json; charset=utf-8',
xhrFields: {
withCredentials: true
},
crossDomain: true,
headers: {
'Authorization': 'Basic ' + authorizationBasic,
},
success: function (result) {
console.log(result);
},
error: function (req, status, error) {
console.log(error);
}
});
What I have done so far:
Public Sub ConsultaFrete()
Dim job As cJobject, jo As cJobject, cj As cJobject
Dim body As String
Dim xmlhttp As New MSXML2.XMLHTTP60
Dim myurl As String
'Call OptimizeCode_Begin
myurl = "https://api.braspress.com/v1/cotacao/calcular/json"
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
xmlhttp.Open "POST", myurl, False
Set job = New cJobject
body = ""
xmlhttp.setRequestHeader "Authorization: Basic", "MDU5MTAxNjIwMDAxODA6UG93ZGVybWl4MkAh"
xmlhttp.setRequestHeader "Content-type", "application/json; charset=utf-8"
With job.init(Nothing, "")
.add "cnpjRemetente", "05910162000180" 'Chave primaria número exclusivo
.add "cnpjDestinatario", "05198251000146"
.add "modal", "R"
.add "tipoFrete", "1"
.add "cepOrigem", "11015201"
.add "cepDestino", "48903500"
.add "vlrMercadoria", "486.00"
.add "peso", "14.00"
.add "volumes", "2"
With .add("cubagem").addArray.add
.add "altura", "0.23"
.add "largura", "0.44"
.add "comprimento", "0.65"
.add "volumes", "2"
End With
End With
body = job.serialize
body = Mid(body, 2, Len(body) - 2)
Debug.Print body
xmlhttp.send body
Debug.Print xmlhttp.responseText
End Sub
Thank you very much in advance