Parsing String to Add to Url-Encoded Url

Parsing string to add to URL-encoded URL

In 2019, URI.encode is obsolete and should not be used.



require 'uri'

URI.encode("Hello there world")
#=> "Hello%20there%20world"
URI.encode("hello there: world, how are you")
#=> "hello%20there:%20world,%20how%20are%20you"

URI.decode("Hello%20there%20world")
#=> "Hello there world"

Create new Uri using Url encoded string in C#

Based on locale settings and used keyboard, iOS may use some other apostrophe-like character, for example right single quotation mark U+2019 (').
Solution was to handle that.

How to encode and parse / decode a nested query string Javascript

So, the qs library proved to be my savior after all. I just wasn't implementing it correctly before. So, with the same form data structure, just make sure to import qs to your form component file:

import qs from 'qs'

and then make your encode function nice and succinct with:

     // Transforms the form data from the React Hook Form output to a format Netlify can read
const encode = (data) => {
return qs.stringify(data)
}

Next, use this encode function in your handle submit function for the form:

 // Handles the post process to Netlify so we can access their serverless functions
const handlePost = (formData, event) => {
event.preventDefault()

fetch(`/`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: encode({ "form-name": 'add-registration-form', ...formData }),
})
.then((response) => {
reset()
if(response.status === 200) {
alert("SUCCESS!")
} else {
alert("ERROR!")
}
console.log(response)
})
.catch((error) => {
console.log(error)
})
}

Finally, this is what your Netlify submission-created.js file should look like more or less:

const sanityClient = require("@sanity/client")
const client = sanityClient({
projectId: process.env.GATSBY_SANITY_PROJECT_ID,
dataset: process.env.GATSBY_SANITY_DATASET,
token: process.env.SANITY_FORM_SUBMIT_TOKEN,
useCDN: false,
})
const qs = require('qs')
const { nanoid } = require('nanoid');

exports.handler = async function (event, context, callback) {

// Pulling out the payload from the body
const { payload } = JSON.parse(event.body)


// Checking which form has been submitted
const isAddRegistrationForm = payload.data.formId === "add-registration-form"

// Build the document JSON and submit it to SANITY
if (isAddRegistrationForm) {
const parsedData = qs.parse(payload.data)

let schedule = parsedData.days
.map(d => (
{
_key: nanoid(),
_type: "classDayTime",
day: d.day,
time: {
_type: "timeRange",
start: d.start,
end: d.end
}
}
))

const addRegistrationForm = {
_type: "addRegistrationForm",
submitDate: new Date().toISOString(),
_studentId: parsedData._id,
classType: parsedData.classType,
schedule: schedule,
language: parsedData.language,
classSize: parsedData.size,
}
const result = await client.create(addRegistrationForm).catch((err) => console.log(err))
}

callback(null, {
statusCode: 200,
})
}

JavaScript parsing urlencoded query parameter

You're decoding the parts before splitting on =, which means %3d is being treated like a key-value separator, ending up in hash[2] inside the loop. This seems to be undesired behaviour, so the fix is to decodeURIComponent after all the .splits


Tried implementing foo[] as list functionality similar to what you find in php, posting for prosperity..

function getUrlVars() {
var $_GET = Object.create(null), // for..in safe
pairs = window.location.search.slice(1).split('&'),
pair,
i;
for (i = 0; i < pairs.length; ++i) {
pair = pairs[i].split('=');
if (pair[0].slice(-2) === '[]') {
pair[0] = pair[0].slice(0, -2);
pair[0] = decodeURIComponent(pair[0]);
if (pair[1]) pair[1] = decodeURIComponent(pair[1]); // don't assume this
if (!(pair[0] in $_GET))
$_GET[pair[0]] = [];
else if (!Array.isArray($_GET[pair[0]]))
$_GET[pair[0]] = [$_GET[pair[0]]];
$_GET[pair[0]].push(pair[1]);
} else {
pair[0] = decodeURIComponent(pair[0]);
if (pair[1]) pair[1] = decodeURIComponent(pair[1]); // don't assume this
$_GET[pair[0]] = pair[1];
}
}
return $_GET;
}

Use on a URL with search ?foo=bar%3d&baz[]=fizz&baz[]=buzz will give

getUrlVars(); // {"foo": "bar=", "baz": ["fizz", "buzz"]}

How to parse string into url.Values in golang?

You could use url.ParseQuery to convert the raw query to url.Values with unescaping

package main

import (
"fmt"
"net/url"
)

func main() {
t := "honeypot=&name=Zelalem+Mekonen&email=zola%40programmer.net&message=Hello+And+this+is+a+test+message..."
v, err := url.ParseQuery(t)
if err != nil {
panic(err)
}

fmt.Println(v)
}

Result:

map[honeypot:[] name:[Zelalem Mekonen] email:[zola@programmer.net] message:[Hello And this is a test message...]]

Parse URLencoded GET string in R into a list

This is a start, not very general really either probably

library(RCurl)
string <- "http://stuff.com/test.aspx?title=Pancreas%20Cancer&startYear=2000&dataCSV=461%2C520%2C559%2C627%2C1065%2C1217%2C1323%2C1512%2C2160%2C2554%2C2707%2C3000%2C3495%2C4163%2C4927%2C5237%2C4785%2C5559%2C6490%2C7559%2C5106%2C6358%2C6824%2C7980%2C4873%2C6715%2C7038%2C8156%2C4863%2C6460%2C7244%2C9161%2C5237%2C6176%2C6531%2C9068%2C5628%2C5983%2C5871%2C7951%2C6060%2C6089%2C5520%2C6627%2C5722%2C6099%2C5586%2C5822%2C4185%2C4909%2C5053%2C5273%2C5227%2C6077%2C6681%2C7977%2C"
string <- URLdecode(string)
string <- strsplit(string, "\\?")[[1]][[2]]
lapply(strsplit(string, "&")[[1]], function(x){
tmp <- strsplit(x, "=")
val <- tmp[[1]][[2]]
names(val) <- tmp[[1]][[1]]
as.list(val)
})

[[1]]
[[1]]$title
[1] "Pancreas Cancer"


[[2]]
[[2]]$startYear
[1] "2000"


[[3]]
[[3]]$dataCSV
[1] "461,520,559,627,1065,1217,1323,1512,2160,2554,2707,3000,3495,4163,4927,5237,4785,5559,6490,7559,5106,6358,6824,7980,4873,6715,7038,8156,4863,6460,7244,9161,5237,6176,6531,9068,5628,5983,5871,7951,6060,6089,5520,6627,5722,6099,5586,5822,4185,4909,5053,5273,5227,6077,6681,7977,"


Related Topics



Leave a reply



Submit