+1 vote
by
Good afternoon, came across the need to pass Form Data and User-Agent in axios post request.

On the site I'm querying, the mobile user-agent needs to pass Form-Data.
5f7afb04d0084528331441.png


But Unfortunately, I do not know how to make a request with a specific user-agent and pass the desired form-data, or rather, attempts have been made, but they failed, so the occasion, please help me knowledgeable people, with me as usual thanks a lot ;)

1 Answer

0 votes
by
// `headers` are custom headers to be sentheaders: {'X-Requested-With': 'XMLHttpRequest'}

// `data` is the data to be sent as the request body// Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'// When no `transformRequest` is set, must be of one of the following types:// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams// - Browser only: FormData, File, Blob// - Node only: Stream, Bufferdata: { firstName: 'Fred'}
https://github.com/axios/axios#request-config
by
Yes, but I'm supposed to get the answer as an array, but it's not there, maybe it's my fault.

The code if you want to help is axios = require('axios')

const getItem = async () => {
const res = await axios.get('https://www.supremenewyork.com/mobile_stock.json')
const data = await res.data
const products_and_categories = data['products_and_categories']['new']

for (let item of products_and_categories) {
if (item['name'].indexOf('Marble Silk S/S Shirt') != -1) {
itemId = item['id']
}
}

return itemId
}

const getStyle = async () => {
const id = await getItem()
const res = await axios.get(`https://www.supremenewyork.com/shop/${id}.json`)
const data = res.data

for (let value of data['styles']) {
if (value['name'].indexOf('Purple') != -1) {
for (let size of value['sizes']) {
if (size['name'].indexOf('Small') != -1 && size['stock_level'] > 0) {
itemStyle = {
'id': id,
'color': value['id'],
'size': size['id']
}
break

} else if (size['name'].indexOf('Medium') != -1 && size['stock_level'] > 0) {
itemStyle = {
'id': id,
'color': value['id'],
'size': size['id']
}
break

} else if (size['name'].indexOf('Large') != -1 && size['stock_level'] > 0) {
itemStyle = {
'id': id,
'color': value['id'],
'size': size['id']
}
break

} else if (size['name'].indexOf('Xlarge') != -1 && size['stock_level'] > 0) {
itemStyle = {
'id': id,
'color': value['id'],
'size': size['id']
}
break

}
}
}
}

return itemStyle
}

const addToBakset = async () => {
const item = await getStyle()
const id = await item['id']
// const res = axios.post(`https://www.supremenewyork.com/shop/${id}/add.json`)
const res = await axios({
url: `https://www.supremenewyork.com/shop/${id}/add.json`,
method: 'post',
headers: {'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
'content-type': 'application/x-www-form-urlencoded',
},
data: {
size: item['size'],
style: item['color'],
qty: 1
}
})

console.log(res.data)
}

addToBakset()
by
And this is what the answer should look like5f7b012ae7fea149540826.png
...