Skip to content
Snippets Groups Projects
Commit 25314fb3 authored by Hugo NOUTS's avatar Hugo NOUTS
Browse files

Parsing var url from enedis body response

parent 80edb3d5
Branches
No related tags found
No related merge requests found
Pipeline #5002 failed
...@@ -35,7 +35,61 @@ func main() { ...@@ -35,7 +35,61 @@ func main() {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Printf("%s\n", string(contents)) // Get the response body as a string
pageContent := string(contents)
// Find a substr
codeStartIndex := strings.Index(pageContent, "?code=")
if codeStartIndex == -1 {
fmt.Println("No code found")
}
codeStartIndex += 6
// Find the index of the closing tag
codeEndIndex := strings.Index(pageContent, "&state=")
if codeEndIndex == -1 {
fmt.Println("No closing tag for code found.")
}
stateStartIndex := strings.Index(pageContent, "&state=")
if stateStartIndex == -1 {
fmt.Println("No state found")
}
stateStartIndex += 7
stateEndIndex := strings.Index(pageContent, "&usage_point_id=")
if stateEndIndex == -1 {
fmt.Println("No closing tag for state found.")
}
usageStartIndex := strings.Index(pageContent, "&usage_point_id=")
if usageStartIndex == -1 {
fmt.Println("No usage found")
}
usageStartIndex += 16
usageEndIndex := strings.Index(pageContent, "&usage_point_id=")
if usageEndIndex == -1 {
fmt.Println("No closing tag for usage found.")
}
usageEndIndex += 30
pageCode := []byte(pageContent[codeStartIndex:codeEndIndex])
pageState := []byte(pageContent[stateStartIndex:stateEndIndex])
pageUsage := []byte(pageContent[usageStartIndex:usageEndIndex])
// Print out the result
fmt.Printf("Page code: %s\n", pageCode)
fmt.Printf("Page state: %s\n", pageState)
fmt.Printf("Page usage: %s\n", pageUsage)
state := strings.Split(pageState, "-")[0]
host := strings.Split(pageState, "-")[1]
redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage
fmt.Println(redir)
http.Redirect(w, r, redir, 302)
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment