I need make request from JavaScript, but I only know how to make it with curl
:
curl https://example.com/api -u test_123: -d source=123 -d description="Aaaa Bbbb" -d email="aaaa@example.com"
I know that -d
is a POST parameter, so I can make:
fetch(`${config.baseUrl}/api/users/profile/`,
{ method: 'POST',
data: JSON.stringify({source: 123, description: 'Aaaa Bbb', email: 'aaaa@example.com'})
})
.then(response => {
// do something
})
How can I set -u
user? Is it a header?
Update
I've made a request in debug mode and get the following output:
> * Server auth using Basic with user 'sk_test_GAaENEsG0PBmvtHBA2g49sPy'
> > POST /v1/customers HTTP/1.1
> > Authorization: Basic c2tfdGVzdF9HQWFFTkVzRzBQQm12dEhCQTJnNDlzUHk6
> > User-Agent: curl/7.35.0
> > Host: api.stripe.com
> > Accept: */*
> > Content-Length: 94
> > Content-Type: application/x-www-form-urlencoded
So the -u
option just converts to base64?
I don't know this fetch
function you're using, and I'm assuming it's part of isomorphic. If its supports a headers
option you can set the Authorization
header with a base64 encoded token:
fetch(`${config.baseUrl}/api/users/profile/`,
{ method: 'POST',
headers: {
"Authorization": "Basic " + window.btoa("test_123:password")
},
data: JSON.stringify({source: 123, description: 'Aaaa Bbb', email: 'aaaa@example.com'})
})
.then(response => {
// do something
})