为什么url.Parse没有填充URL.RawPath?

时间:2016-09-01 11:42:21

标签: url go escaping

虽然输入类似,但URL.RawQuery似乎已正确填充。

Playground

 <editor-for ng-model="merchant.header.logos.smallAbsoluteUrl" is-required="true" data-width="88" data-height="31" editor-title="Logo Url" editor-type="image" is-disabled="!isMasterSector" class="ng-pristine ng-untouched ng-valid ng-isolate-scope ng-not-empty" style=""></editor-for>

2 个答案:

答案 0 :(得分:3)

请注意,URL.RawPath不是原始(转义)路径。 可能,但并非总是如此。它只是一个提示。它的医生说:

RawPath    string // encoded path hint (Go 1.5 and later only; see EscapedPath method)

因此,当您需要转义路径时,请始终使用URL.EscapedPath()并且不要依赖URL.RawPath字段。 URL.EscapedPath()的文件说:

  

EscapedPath返回u.RawPath ,当它是u.Path 的有效转义时。

这是你的情况。如果原始路径包含在URL编码期间需要转义的字节,则该路径无效。您的路径就是这样一个示例,因为它包含%25,它是百分比符号本身'%'的网址转义文字,如果是网址路径的一部分,则需要转义百分比符号。

您的第一个示例包含%2f,它是斜杠'/'的网址转义文本,如果路径中存在,则不需要转义('/'是路径中的有效字符并被视为分隔符。)

见这个例子:

u, err = url.Parse("https://example.com/foo%25fbar?q=morefoo%25bar")
if err != nil {
    log.Fatal(err)
}
fmt.Println("Path:       ", u.Path)
fmt.Println("RawPath:    ", u.RawPath)
fmt.Println("EscapedPath:", u.EscapedPath())
fmt.Println("RawQuery:   ", u.RawQuery)
fmt.Println("String:     ", u.String())

输出(在Go Playground上尝试):

Path:        /foo%fbar
RawPath:     
EscapedPath: /foo%25fbar
RawQuery:    q=morefoo%25bar
String:      https://example.com/foo%25fbar?q=morefoo%25bar

RawPath是空字符串(因为"/foo%25fbar"是无效的转义路径),但EscapedPath()会返回原始的转义路径。

答案 1 :(得分:2)

只需使用fmt.Println(u.EscapedPath()) //give you expected result

即可
// RawPath is a hint as to the encoding of Path to use
// in url.EscapedPath. If that method already gets the
// right answer without RawPath, leave it empty.
// This will help make sure that people don't rely on it in general. 

来自包装规格

  

通常,代码应调用EscapedPath而不是读取u.RawPath   直接

来自源评论

$.ajax({
  url: "http://www.posta.com.tr/api/LiveScore/LeagueStageFixture",
  type: "get", //send it through get method
  headers:{ 
    "Access-Control-Allow-Origin": "*" 
    },
  data:{
        TournamentID: 1,
        includeFixture: 1
    },
  success: function(response) {
    //Do Something
  },
  error: function(xhr) {
    //Do Something to handle error
  }
});