INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: MCWS and Escaping Chars?  (Read 14299 times)

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
MCWS and Escaping Chars?
« on: October 04, 2015, 06:21:00 pm »

I'm having some issues working out how to escape "special" characters.  I'm using the following WinHTTP subroutine:
Code: [Select]
 WinHTTP := ComObjCreate("WinHTTP.WinHttpRequest.5.1")
  ComObjError(false)
  WinHTTP.Open("GET", MC_Call)
  WinHTTP.SetCredentials(MC_UserName,MC_Password,0)
  Body = ""
  WinHTTP.Send(Body)
  Result := WinHTTP.ResponseText
  Status := WinHTTP.Status

This all works fine except when the content of the "MC_Call" has a special char like "&" in one of the values at which point I gt a "500" error, eg:

Works : MCWS/v1/File/SetInfo?File=12345&FileType=Key&Field=Name, Track #&Value=This and That,1&List=CSV
Fails   : MCWS/v1/File/SetInfo?File=12345&FileType=Key&Field=Name, Track #&Value=This & That,1&List=CSV

I've tried a few ways of Escaping the & but wrapping it like "&" and %22&%22 so I don't know if I have the syntax correct of if I to set the Request Header like the following (which does not help!):
  WinHTTP.SetRequestHeader("Content-Type", "text/xml;charset=UTF-8")

Thanks
Nathan

Edit - Cross posed at the AHK forum
Logged
JRiver CEO Elect

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8009
Re: MCWS and Escaping Chars?
« Reply #1 on: October 04, 2015, 06:43:32 pm »

I'm guessing you need to URL encode anything that's not just alphanumeric.  You know the %20 things you see in URLs sometimes?  (%20 is a space in URL encoding).

Look here for some information and a place where you can type in stuff and have it URL encoded for you:

http://www.w3schools.com/tags/ref_urlencode.asp

Brian.
Logged

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
Re: MCWS and Escaping Chars?
« Reply #2 on: October 04, 2015, 08:20:08 pm »

Thanks Brian,
Replacing the & with %26 works with my current setup, but there must be a way of the ComObj to do the URL encoding, eg something like adding:
  WinHTTP.SetRequestHeader("Content-type", "application/x-www-form-urlencoded")

(which by the way does not work)
Logged
JRiver CEO Elect

blgentry

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 8009
Re: MCWS and Escaping Chars?
« Reply #3 on: October 04, 2015, 09:49:22 pm »

A lot of programming languages have libraries for automating URL Encoding.  Perhaps the one you're using has a function you can call to process your strings into URL Encoded strings.  Then you could pass the encoded string the MCWS.  As I understand it, this is how a lot of web centric programs work.

Brian.
Logged

Hendrik

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 10715
Re: MCWS and Escaping Chars?
« Reply #4 on: October 05, 2015, 02:35:36 am »

WinHTTP does not have such functions, especially because on its level, you cannot do escaping anymore anyway.

Consider this URL:
MCWS/v1/File/SetInfo?File=12345&FileType=Key&Field=Name, Track #&Value=This & That,1&List=CSV

You need to escape & (and strictly speaking the spaces as well), but which & needs escaping? All of them? No, that wouldn't work.
Thats why such APIs require you to manually escape the individual arguments of a URL before passing it to the Open function. So you need to escape the Value specifically, without affecting the other &'s in the URL, ie. before assembling the final URL.
Logged
~ nevcairiel
~ Author of LAV Filters

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
Re: MCWS and Escaping Chars?
« Reply #5 on: October 05, 2015, 03:45:37 am »

Thanks.  I had tried encoding the whole string but that of course (as Hendrik said) stuffs it all up and I finally got to the understanding that I just need to correctly encode the "value" part I'm sending.  Makes sense once you know.  ::)  I've tracked down a URL encoding function so I'll plug that into my script as see how it goes.

Anyway, I've run out of time for a few days, but I'd already been playing around prior to see what common "special" CHARs need escaping.  Most were fine as is, some worked by substituting say %26 for &, but others like " or , don't work and neither does their %22 or %2c equivalent.  I also found that my same HTTP call to MCWS gets all the special char I tried encoded correctly to a Text File with the exception of & that comes back as & which I thought was odd.

Nathan
Logged
JRiver CEO Elect

mwillems

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 5175
  • "Linux Merit Badge" Recipient
Re: MCWS and Escaping Chars?
« Reply #6 on: October 05, 2015, 07:30:50 am »

I also found that my same HTTP call to MCWS gets all the special char I tried encoded correctly to a Text File with the exception of & that comes back as & which I thought was odd.

Nathan

FWIW I saw the same behavior in some linux MCWS scripting I've been doing, so I think that's to be expected.  I just used a text processing utility (sed) to scrub and replace &amp with & in the text file.  I haven't hit any other characters that do something like that yet, but I'm sure they're out there.

Logged

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
Re: MCWS and Escaping Chars?
« Reply #7 on: October 20, 2015, 04:32:08 pm »

I'm still throwing an error from MCWS trying to pass a URL encoded comma, eg my original string is:
    Like Father, Like Son.

I'm encoded the String for the MCWS call as follows but it throws an Error - 500 Procssed Failed on the comma
    Like%20Father%2C%20Like%20Son%2E

Any hints on how to encode a comma?

Thanks
Nathan
Logged
JRiver CEO Elect

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
Re: MCWS and Escaping Chars?
« Reply #8 on: October 20, 2015, 04:56:29 pm »

I have also tried Escaping the whole string but the following also fails:
  /#Like Father, Like Son.#/
  /#Like%20Father%2C%20Like%20Son%2E#/

I think it is only the Comma I'm struggling with

Logged
JRiver CEO Elect

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
Re: MCWS and Escaping Chars?
« Reply #9 on: October 20, 2015, 05:35:44 pm »

Ok - think I worked it out.  URL encode the sting and wrap it in Quotes, like:
  "Like%20Father%2C%20Like%20Son%2E"
Logged
JRiver CEO Elect

Hendrik

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 10715
Re: MCWS and Escaping Chars?
« Reply #10 on: October 20, 2015, 05:38:14 pm »

Thats because the comma is a field separator in MC. Its not a HTTP thing. Just in case you are wondering.
Logged
~ nevcairiel
~ Author of LAV Filters

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14267
  • I won! I won!
Re: MCWS and Escaping Chars?
« Reply #11 on: October 20, 2015, 06:28:06 pm »

Thanks - I just did not know how to escape it correctly in combination with the encoding so that it was read as part of the value not as a delimiter.  All good now.
Logged
JRiver CEO Elect
Pages: [1]   Go Up