Multipart/Form-Data POST 요청을 사용할 때 301 Moved Permanently 오류가 발생하는 이유는 무엇입니까?
Multipart/Form-Data POST
multipart/form-data를 사용하여 POST 데이터를 시도할 때 제공된 것과 같은 오류 메시지가 나타날 수 있습니다. 마주쳤다. 문제를 이해하려면 문제의 구성을 조사해야 합니다. 발생한 오류는 301 Moved Permanently 응답으로, 리소스가 영구적으로 리디렉션되었음을 나타냅니다. 이 문제는 multipart/form-data 요청에 대해 올바른 Content-Type 헤더가 설정되지 않은 경우에 자주 발생합니다.
이 문제를 해결하려면 Content-Type 헤더가 명시적으로 "multipart/form-data; POST 요청을 할 때 charset=UTF-8"입니다. 이 헤더는 요청에 multipart/form-data 프로토콜에 따라 형식이 지정된 텍스트 기반 데이터와 바이너리 데이터가 모두 포함되어 있음을 서버에 알립니다.
다음은 올바른 Content-Type 헤더를 성공적으로 설정하는 수정된 Go 코드입니다.
<code class="go">import ( "bytes" "fmt" "io" "io/ioutil" "mime/multipart" "net/http" ) func NewPostWithMultipartFormData(url string, paramTexts map[string]string, paramFiles []FileItem) ([]byte, error) { // Initialize a buffer to write the multipart form data. buf := new(bytes.Buffer) // Create a new multipart writer. w := multipart.NewWriter(buf) // Add text parameters to the multipart form. for key, value := range paramTexts { field, err := w.CreateFormField(key) if err != nil { return nil, fmt.Errorf("error creating form field '%s': %v", key, err) } if _, err := field.Write([]byte(value)); err != nil { return nil, fmt.Errorf("error writing value to form field '%s': %v", key, err) } } // Add binary parameters to the multipart form. for _, file := range paramFiles { fileWriter, err := w.CreateFormFile(file.Key, file.FileName) if err != nil { return nil, fmt.Errorf("error creating form file '%s': %v", file.Key, err) } if _, err := fileWriter.Write(file.Content); err != nil { return nil, fmt.Errorf("error writing content to form file '%s': %v", file.Key, err) } } // Close the multipart writer. if err := w.Close(); err != nil { return nil, fmt.Errorf("error closing multipart writer: %v", err) } contentType := w.FormDataContentType() // Create a new POST request with the correct Content-Type header. req, err := http.NewRequest(http.MethodPost, url, buf) if err != nil { return nil, fmt.Errorf("error creating HTTP request: %v", err) } req.Header.Set("Content-Type", contentType) // Perform the HTTP request. client := http.Client{} resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("error sending HTTP request: %v", err) } defer resp.Body.Close() // Read the response body. body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("error reading HTTP response body: %v", err) } return body, nil }</code>
위 내용은 Multipart/Form-Data POST 요청을 사용할 때 301 Moved Permanently 오류가 발생하는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

InterfacesandPolymorphismingoEnhancecodereusabilitableandabledaysainability.

theinitfunctionorunsautomically weconitializepackages 및 seteptheenvironment.ituplopgortingupglobalvariables, andperformingone-timesetupstasksacrossanypackage

인터페이스 조합은 기능을 작고 집중된 인터페이스로 분류하여 GO 프로그래밍에서 복잡한 추상화를 구축합니다. 1) 독자, 작가 및 더 가까운 인터페이스를 정의하십시오. 2) 이러한 인터페이스를 결합하여 파일 및 네트워크 스트림과 같은 복잡한 유형을 만듭니다. 3) ProcessData 함수를 사용하여 이러한 결합 된 인터페이스를 처리하는 방법을 보여줍니다. 이 접근법은 코드 유연성, 테스트 가능성 및 재사용 성을 향상 시키지만 과도한 조각화 및 조합 복잡성을 피하기 위해주의를 기울여야합니다.

inittectionsingoareautomaticallyCalledBeforeMainForeChalledBectOnforTeForTupButcomewithChalleds

기사는 이동 중에지도를 통한 반복, 안전한 관행, 항목 수정 및 대규모지도에 대한 성능 고려 사항에 중점을 둡니다.

이 기사에서는 크기, 메모리 할당, 기능 통과 및 사용 시나리오에 중점을 둔 배열과 슬라이스의 차이점에 대해 설명합니다. 배열은 고정 크기, 스택-할당되며 슬라이스는 역동적이며 종종 힙 할당되며 유연합니다.

이 기사에서는 리터럴 사용, Make Function, 기존 배열 또는 슬라이스를 포함하여 GO에서 슬라이스를 작성하고 초기화하는 것에 대해 설명합니다. 또한 슬라이스 구문과 슬라이스 길이와 용량을 결정합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
