bndr / gojenkins

Jenkins API Client in Go. Looking for maintainers to move this project forward.
Apache License 2.0
869 stars 446 forks source link

UpdateConfig displays garbled characters when using Chinese #320

Open nxk88998 opened 8 months ago

nxk88998 commented 8 months ago
    config = fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<flow-definition plugin="workflow-job@2.31">
 <description></description>
 <keepDependencies>false</keepDependencies>
 <properties/>
 <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.50">
    <script>
测试
</script>
    <sandbox>true</sandbox>
 </definition>
 <triggers/>
 <disabled>false</disabled>
</flow-definition>`)

     job.UpdateConfig(ctx, config)

测试 >> Jenkins displays garbled code >> 测è¯

nxk88998 commented 8 months ago

已修复,添加charset=utf-8到Content-Type标头显式指定字符编码修复 func (r *Requester) PostXML(ctx context.Context, endpoint string, xml string, responseStruct interface{}, querystring map[string]string) (*http.Response, error) { payload := bytes.NewBuffer([]byte(xml)) ar := NewAPIRequest("POST", endpoint, payload) if err := r.SetCrumb(ctx, ar); err != nil { return nil, err } ar.SetHeader("Content-Type", "application/xml; charset=utf-8") ar.Suffix = "" return r.Do(ctx, ar, &responseStruct, querystring) }

ylighgh commented 5 months ago

如何解决的

nxk88998 commented 3 months ago

如何解决的

request.go文件中修改PostXML函数添加默认charset=utf-8

完整代码 func (r Requester) PostXML(ctx context.Context, endpoint string, xml string, responseStruct interface{}, querystring map[string]string) (http.Response, error) { payload := bytes.NewBuffer([]byte(xml)) ar := NewAPIRequest("POST", endpoint, payload) if err := r.SetCrumb(ctx, ar); err != nil { return nil, err } ar.SetHeader("Content-Type", "application/xml; charset=utf-8") ar.Suffix = "" return r.Do(ctx, ar, &responseStruct, querystring) }