Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

axiosのヘッダーのconfigでちょっとハマった

$
0
0

某APIを試しててaxiosのconfig指定をミスってたので、自戒の意味を込めて残しておきます。

完全に自分用メモっぽいやつです。

ミスったコードなど

//省略classHoge{constructor(){//省略}//ミスった方methodA(IMAGE_PATH){constfile=fs.createReadStream(IMAGE_PATH);constform=newFormData();form.append('image',file);form.append('entrance','detection');constconfig={'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders()};returnaxios.post(this.URL.RECOGNITION,form,config)}//うまくいった方methodB(IMAGE_PATH){constfile=fs.createReadStream(IMAGE_PATH);constform=newFormData();form.append('image',file);form.append('entrance','detection');constconfig={headers:{'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders(),}}returnaxios.post(this.URL.RECOGNITION,form,config)}}consthoge=newHoge();hoge.methodA();// エラーhoge.methodB();// 成功

axiosのconfigミス

慣れてつかってるうちにconfig=headersっぽいイメージで使ってたみたい。
通常configの中には他にもmethod指定やbodyデータなども入ってくるのでconfig.headersな指定にしないとですね。

constconfig={'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders()};
constconfig={headers:{'X-ClovaOCR-Service-ID':this.SERVICE_ID,...form.getHeaders(),}}

Viewing all articles
Browse latest Browse all 8691

Trending Articles